# _*_ coding:utf-8 _*_
# @Time  : 2022-02-16
# @Author: zizle


# 获取需要的数据，对接wind接口。然后保存到服务器
import time
import datetime
import requests
import urllib3
from WindPy import w
urllib3.disable_warnings()


# 获取需要的指标数据
def get_need_indexes(frequency):
    url = 'http://127.0.0.1:8000/v1/datalib/table/' if LOCAL else 'https://210.13.218.130:9000/v1/datalib/table/'
    params = {
        'page': 1,
        'page_size': 9999,
        'frequency': frequency,
        'plat': 'wind'
    }
    try:
        r = requests.get(url, params=params, verify=False)
        need_indexes = r.json()['data']['records']
    except Exception as e:
        print(f'获取需要的更新的指标数据失败了:{e}...')
        return []
    else:
        if FRESH_NEW:
            need_indexes = list(filter(lambda x: not x['refresh'], need_indexes))
        return need_indexes


# 进度打印
def print_progress_bar(current, total, color=35, title=''):  # percent是小数
    percent = round(current/total, 4)
    prefix_progress = '▓' * int(80 * percent)
    leave_progress = '▓' * (80 - int(80 * percent))
    # percent = '%.2f' % (percent * 100)
    print(f'\033[1;{color}m{current}/{total}\033[0m|\033[1;{color}m{prefix_progress}\033[0m{leave_progress} {title}')


# 获取wind数据
def get_wind_data(indexes_list):
    w.start()
    if not w.isconnected():
        print('WindPy连接失败!')
        return
    print('WIndPy登录成功!')
    total_count = len(indexes_list)
    for i, index_item in enumerate(indexes_list):
        if not FRESH_NEW and index_item['state'] == 0 and index_item['refresh']:  # 停止更新的不再请求数据
            continue

        if pointers and index_item['source_id'] not in pointers:
            continue

        start = index_item["enddate"] if index_item["enddate"] else index_item["startdate"]
        # print(f'获取Wind - {index_item["source_id"]}数据,起始日期:{start}')
        ret = w.edb(index_item['source_id'], start, Fill='Previous', usedf=True)
        if ret[0] != 0:
            print(f'\033[1;34m获取 {index_item["source_id"]} 数据, 起始日期:{start}.失败了，code={ret[0]} \033[0m')
            if str(ret[0]) == '-40522015':
                print(f'无{index_item["source_id"]}的数据权限!')
            continue
        ret_df = ret[1]
        if ret_df.shape[0] == 1:  # 只有一条数据的时候，返回指标为index,也无需保存数据(因为每次至少获取上一日和本日数据)
            if refresh_now:
                save_data_to_server(index_item['id'], datalist=[])  # 刷新更新的日期
                msg = f'指标{index_item["source_id"]},最新日期:{start}已存,刷新成功.'
                time.sleep(1)
            else:
                msg = f'指标{index_item["source_id"]},最新日期:{start}已存,略...'
                time.sleep(0.4)
            print_progress_bar(current=i+1, total=total_count, color=36, title=msg)
            continue
        # 转为想要的数据格式
        ret_df['datadate'] = ret_df.index
        ret_df['datadate'] = ret_df['datadate'].apply(lambda x: x.strftime('%Y-%m-%d'))
        ret_df.rename(columns={'CLOSE': 'datavalue'}, inplace=True)

        save_data_to_server(index_item['id'], ret_df.to_dict(orient='records'))
        msg = f'指标{index_item["source_id"]},起始日期:{start}保存完成.'
        print_progress_bar(current=i+1, total=total_count, color=35, title=msg)
        time.sleep(0.65)

    w.stop()


# 更新一个指标
def save_data_to_server(sheet_id, datalist):
    url = f'http://127.0.0.1:8000/v1/datalib/table/update/' if LOCAL else \
        f'https://210.13.218.130:9000/v1/datalib/table/update/'
    body_data = {
        'dataid': sheet_id,
        'row_value': datalist
    }
    try:
        r = requests.post(url, json=body_data, verify=False)
        if r.json()['code'] != 200:
            raise ValueError(f'status_code={r.json()["code"]}')
    except Exception as e:
        if datalist:
            msg = f'保存数据指标 ID={sheet_id}失败了：{e}'
            # print(r.json())
        else:
            msg = f'更新指标 ID={sheet_id} 刷新日期失败了：{e}'
        print(msg)
    else:
        pass


# 过滤无需更新的指标
def filter_unwanted_index(item, frequency):
    if FRESH_NEW:
        return True
    if not item['enddate']:
        return True
    value_date = datetime.datetime.strptime(item['enddate'], '%Y-%m-%d')
    if frequency == '日':
        return value_date.strftime('%Y%m%d') != TODAY.strftime('%Y%m%d')
    elif frequency == '周':
        return value_date.strftime('%W') != TODAY.strftime('%W')
    elif frequency == '月':  # 数据更新到上个月就不用更新
        return (int(TODAY.strftime('%Y%m')) - int(value_date.strftime('%Y%m'))) > 1
    elif frequency == '年':  # 数据更新到上一年，就不用更新
        return (int(TODAY.strftime('%Y')) - int(value_date.strftime('%Y'))) > 1
    else:
        return True


def start_log(text):
    with open('start.log', 'a+') as f:
        f.write('\n{} - {}!'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), text))


if __name__ == '__main__':
    start_log('程序启动')
    while True:
        # if datetime.datetime.now().strftime('%H%M') in ['0720', '1525', '1615', '1643', '1715']:
        if datetime.datetime.now().strftime('%H%M') in ['1830']:
            # for f in ['日', '周', '月', '季', '年']:
            for f in ['日']:
                start_log('API接口启动...')
                TODAY = datetime.datetime.today()
                LOCAL = False
                FRESH_NEW = False
                pointers = []  # 'S9986235', 'S9987644'百川盈孚的数据，没有权限
                FREQUENCY = f
                refresh_now = False
                indexes = get_need_indexes(frequency=FREQUENCY)
                indexes.reverse()
                print('获取WIND指标数量:', len(indexes))
                indexes = list(filter(lambda x: filter_unwanted_index(x, FREQUENCY), indexes))  # 过滤指标
                print('待更新WIND指标数量:', len(indexes))
                get_wind_data(indexes)
                print('程序结束,WindPy退出!')
                start_log('API数据更新完成！')
                time.sleep(15)
        time.sleep(1)
